package org.dromara.chicken.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.chicken.domain.ChickenReduceInfo;
import org.dromara.chicken.mapper.ChickenReduceInfoMapper;
import org.dromara.chicken.service.IChickenReduceInfoService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 减少记录Service业务层处理
 *
 * @author Mr Lei QQ235921049
 * @date 2024-06-10
 */
@RequiredArgsConstructor
@Service
public class ChickenReduceInfoServiceImpl implements IChickenReduceInfoService {

    private final ChickenReduceInfoMapper baseMapper;

    /**
     * 查询减少记录
     */
    @Override
    public ChickenReduceInfo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询减少记录列表
     */
    @Override
    public TableDataInfo<ChickenReduceInfo> queryPageList(ChickenReduceInfo bo, PageQuery pageQuery) {
        Page<ChickenReduceInfo> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询减少记录列表
     */
    @Override
    public List<ChickenReduceInfo> queryList(ChickenReduceInfo bo) {
        LambdaQueryWrapper<ChickenReduceInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ChickenReduceInfo> buildQueryWrapper(ChickenReduceInfo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChickenReduceInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq( ChickenReduceInfo::getOrderId, bo.getOrderId());
        lqw.eq(bo.getHouseId() != null, ChickenReduceInfo::getHouseId, bo.getHouseId());
        lqw.like(bo.getHatchId() != null, ChickenReduceInfo::getHatchId, bo.getHatchId());
        return lqw;
    }

    /**
     * 新增减少记录
     */
    @Override
    public Boolean insertByBo(ChickenReduceInfo bo) {
        ChickenReduceInfo add = MapstructUtils.convert(bo, ChickenReduceInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改减少记录
     */
    @Override
    public Boolean updateByBo(ChickenReduceInfo bo) {
        ChickenReduceInfo update = MapstructUtils.convert(bo, ChickenReduceInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChickenReduceInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除减少记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
